package com.wwwarehouse.carddesk.fragment.teamfragment.commenttargetweb;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.alibaba.fastjson.JSON;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskGroupGetGroupBean;
import com.wwwarehouse.carddesk.bean.GroupHtmlDataBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.fragment.CommonBridgeWebViewFragment;
import com.wwwarehouse.common.tools.ToastUtils;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/11/16 14:49
 * @描述 ${TODO}
 */

public class QualityTargetFragmnent extends CommonBridgeWebViewFragment {
    private String mBaseUrl = "";
    private int mIsTriMember;
    private String[] mUps;
    private String[] mDowns;
    @Override
    public void requestDatas() {
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));
        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = "";
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_RELEASE;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_CIT2;
        }

        /**
         * effective.html    效率
         * quality.html    品质
         * aging.html      时效
         */
        String newUrl = mBaseUrl + CardDeskConstant.PATH_GROUP_QUALITY_H5_URL;

        bundleBean = getArguments().getParcelable("bean");
        mWebView.loadUrl(newUrl);


        EventBus.getDefault().register(this);
        mFlipView.setOnClickListener(this);
        mRlOpposite.setVisibility(View.GONE);
        mRlContent.setVisibility(View.INVISIBLE);
        mStateLayout.setVisibility(View.VISIBLE);
        mRlComplaints.setOnClickListener(this);
        mRlPrompt.setOnClickListener(this);
        mRlCollection.setOnClickListener(this);

        mFlipView.setVisibility(View.GONE);
        //        ((BaseCardDeskActivity) mActivity).showRefreshBt();
        //        ((BaseCardDeskActivity) mActivity).hideScanFgBt();
        ((BaseCardDeskActivity) mActivity).hideRefreshBt();
        ((BaseCardDeskActivity) mActivity).showScanFgBt();

        mStateLayout.showProgressView(false);
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if(newProgress == 100){
                    mStateLayout.showContentView();
                }else{
                }
            }
        });
        if (getArguments() != null) {
            mIsTriMember = getArguments().getInt("IsTriMember");
            if (getArguments().getStringArray("up") != null && getArguments().getStringArray("down") != null) {
                mUps = getArguments().getStringArray("up");
                mDowns = getArguments().getStringArray("down");
            }

        }
        registJsMethod();
    }

    private CardDeskGroupGetGroupBean.ListBean bundleBean;
    private String data;

    private void registJsMethod() {

        GroupHtmlDataBean bean = new GroupHtmlDataBean();
        bean.setToken(BaseApplication.sp.getValue(Constant.sp_Token));
        if(bundleBean != null ){
            bean.setRankingCompany(new String[]{bundleBean.getBuId()});
            bean.setRankingUser(new String[]{bundleBean.getUserId()});
            bean.setIsTriMember(mIsTriMember);
            if (mUps != null && mDowns != null) {
                bean.setPrivilegeUp(mUps);
                bean.setPrivilegeDown(mDowns);
            } else {
                bean.setPrivilegeUp(new String[]{});
                bean.setPrivilegeDown(new String[]{});
            }
        }

        data = JSON.toJSON(bean).toString();

        //Js获取请求的参数
        mWebView.callHandler("getData", data, new CallBackFunction() {
            @Override
            public void onCallBack(String responseData) {
                ToastUtils.showToast("" + responseData);
            }
        });

        //隐藏StateLayout和ProgressDialog
        mWebView.registerHandler("hideProgress", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                //                dismissProgressDialog();
                if (mRlContent.getVisibility() != View.VISIBLE) {
                    mRlContent.setVisibility(View.VISIBLE);
                }
                if (mStateLayout.getVisibility() != View.INVISIBLE) {
                    mStateLayout.setVisibility(View.INVISIBLE);
                    mStateLayout.showContentView();
                }
                dismissProgressDialog();
            }
        });



        //显示progressDialog
        mWebView.registerHandler("showProgress", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                //                showProgressDialog("正在加载...");
                mStateLayout.showProgressView(false);
            }
        });

        //显示错误信息
        mWebView.registerHandler("showErrorMsg", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                toast("" + data);
                dismissProgressDialog();
                mStateLayout.showSystemView(data,false);
                mStateLayout.setSystemListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mWebView.reload();
                        mStateLayout.showProgressView(false);
                    }
                });
            }
        });

        //显示没有数据view
        mWebView.registerHandler("showNotData", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                dismissProgressDialog();
                mStateLayout.showEmptyView(data,false);
                mStateLayout.setEmptyListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mWebView.reload();
                        mStateLayout.showProgressView(false);
                    }
                });
            }
        });


    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof QualityTargetFragmnent) {
            mActivity.setTitle(getString(R.string.teamdesk_quality));
        }
    }

    @Override
    public void onEventMainThread(BackListenerEvent event) {
        if (peekFragment() instanceof CommonBridgeWebViewFragment && "CommonBridgeWebViewFragment".equals(event.getMsg())) {
            popFragment();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //            super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                //                    String index = data.getStringExtra("data");
                Log.d("onActivityResult", "index");
                break;
        }
    }
}
